package com.javathread.th1;

public class SleepLock {
	
	private Object lock = new Object();
	
	public static void main(String[] args) {
		SleepLock sleep = new SleepLock();
		Thread thread1 = sleep.new ThreadSleep();
		thread1.setName("ThreadSleep");
		
		Thread thread2 = sleep.new ThreadNotSleep();
		thread2.setName("ThreadNotSleep");
		
		thread1.start();
		
		try {
			Thread.sleep(1000);
			System.out.println("主线程休眠");
		} catch (Exception e) {
			e.printStackTrace();
		}
		thread2.start();
	}
	
	private class ThreadSleep extends Thread{

		@Override
		public void run() {
			String threadName = Thread.currentThread().getName();
			System.out.println(threadName + " 获得 lock *****");
			try {
				synchronized (lock) {
					System.out.println(threadName + " taking the lock -------");
					System.out.println("+++++Finish the work" + threadName);
				}
				Thread.sleep(3000);
			} catch (Exception e) {
			}
		}
		
	}
	
	private class ThreadNotSleep extends Thread{

		@Override
		public void run() {
			String threadName = Thread.currentThread().getName();
			System.out.println(threadName + "获得锁时间="+System.currentTimeMillis() + " =======");
			synchronized (lock) {
				System.out.println(threadName + " taking the lock time =" + System.currentTimeMillis()+"++++++++");
				System.out.println("Finish the work" + threadName);
			}
		}
		
	}
}
